#!/bin/bash

SHELL_DIR="$(dirname "$(readlink -f "$0")")"
cd "$SHELL_DIR"

result=0

pkg_hash="$(mktemp -d)"
for class_dir in class/*; do
  for pkg in $(ls -1 "$class_dir"); do
	sl="$class_dir/$pkg"
	if [ -d "$(readlink -f "$sl")" ]; then
	  if [ -f "$pkg_hash/$pkg" ]; then
		result=1
		echo "Repeated package '$pkg' setting '$(cat $pkg_hash/$pkg)' and '$class_dir'"
	  else
		echo "$class_dir" > "$pkg_hash/$pkg"
	  fi
	else
	  result=1
	  echo "Broken symbolic link $sl -> $(readlink $sl)" >&2
	fi
  done
done
for pkg in $(ls -1 "packages"); do
  if [ ! -f "$pkg_hash/$pkg" ]; then
	result=1
	echo "Not classified package $pkg" >&2
  fi
done
rm -rf "$pkg_hash"

exit $result

